﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;


public partial class EditFlightInfo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    ConnectUnit conn = new ConnectUnit("localhost", "sa", "sa", "FlyWaySystem");
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        StringBuilder condition = new StringBuilder();
        if (cbFlightName.Checked) {
            condition.Append("FlightName='");
            condition.Append(txtFlightName.Text);
            if (cbOrigin.Checked || cbSchedule.Checked || cbDestination.Checked){
                condition.Append("' AND ");
            }
            else
            {
                condition.Append("'");
            }
        }
        if (cbOrigin.Checked) {
            condition.Append("Origin=N'");
            condition.Append(ddlOrigin.Text);
            if (cbDestination.Checked || cbSchedule.Checked)
            {
                condition.Append("' AND ");
            }
            else
            {
                condition.Append("'");
            }
        }
        if (cbDestination.Checked)
        {
            condition.Append("Destination=N'");
            condition.Append(ddlDestination.Text);
            if (cbSchedule.Checked)
            {
                condition.Append("' AND ");
            }
            else{
                condition.Append("'");
            }
        }
        if (cbSchedule.Checked)
        {
            condition.Append("Schedule like '%");
            condition.Append(txtSchedule.Text);
            condition.Append("%'");
        }
        condition.ToString();
        DataSet ds = conn.Select("FlightInformation", condition.ToString());
        if (conn.getRowCount(ds) > 0)
        {
            gvFlightInfo.DataSource = ds;
            gvFlightInfo.DataBind();
            gvFlightInfo.Visible = true;
            btnDelete.Visible = true;
            btnEdit.Visible = true;
            btnNew.Visible = true;
            btnUpdate.Visible = false;
            gvEdit.Visible = false;
            Session["allChoise"] = "false";
        }
        else {
            MessageBox.Show("Không có kết quả nào!");
            gvFlightInfo.Visible = false;
            btnDelete.Visible = false;
            btnEdit.Visible = false;
            btnNew.Visible = false;
        }
        

    }
   
    
    protected void lbtnChoise_Click(object sender, EventArgs e) {
        if (!(Session["allChoise"].ToString()=="true"))
        {
            Session["allChoise"] = "true";
            foreach (GridViewRow gRow in gvFlightInfo.Rows)
            {
                ((CheckBox)gRow.FindControl("cbChoise")).Checked = true;  
            }
        }
        else {
            foreach (GridViewRow gRow in gvFlightInfo.Rows)
            {
                ((CheckBox)gRow.FindControl("cbChoise")).Checked = false; 
            }
            Session["allChoise"] = "false";
        }
        
    }
    
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow gRow in gvFlightInfo.Rows)
        {
            if (((CheckBox)gRow.FindControl("cbChoise")).Checked == true) {
                string i = gRow.Cells[0].Text;
                conn.Delete("FlightInformation", "FlightNo='" + i + "'");
            }
        }
        btnSubmit_Click(sender, e);
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        Response.Redirect("Admin.aspx");
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        String ID = "";
        foreach (GridViewRow gRow in gvFlightInfo.Rows)
        {
            if (((CheckBox)gRow.FindControl("cbChoise")).Checked == true)
            {
                string i = gRow.Cells[0].Text;
                ID = ID + " OR FlightNo='" + i + "'";
            }
        }
        
        if (ID.Length > 15) {
            ID = ID.Substring(4, ID.Length - 4);
            DataSet ds = conn.Select("FlightInformation", ID);
            gvFlightInfo.Visible = false;
            gvEdit.DataSource = ds;
            gvEdit.DataBind();
            gvEdit.Visible = true;
            btnUpdate.Visible = true;
            btnEdit.Visible = false;
        }
        
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in gvEdit.Rows) {
            String ID = row.Cells[0].Text;
            String FlightName = ((TextBox)row.FindControl("txtgFlightName")).Text;
            String Origin = ((TextBox)row.FindControl("txtgOrigin")).Text;
            String Destination = ((TextBox)row.FindControl("txtgDestination")).Text;
            String Schedule = ((TextBox)row.FindControl("txtgSchedule")).Text;
            String FlightTime = ((TextBox)row.FindControl("txtgFlightTime")).Text;
            String TotalSeat = ((TextBox)row.FindControl("txtgTotalSeat")).Text;
            String AvailableSeat = ((TextBox)row.FindControl("txtgAvailableSeat")).Text;
            String Class = ((TextBox)row.FindControl("txtgClass")).Text;
            Dictionary<String,object> valueUpdate = new Dictionary<string,object>();
            valueUpdate.Add("FlightName",FlightName);
            valueUpdate.Add("Origin",Origin);
            valueUpdate.Add("Destination",Destination);
            valueUpdate.Add("Class",Class);
            valueUpdate.Add("Schedule",Schedule);
            valueUpdate.Add("TotalSeat",TotalSeat);
            valueUpdate.Add("AvailableSeat",AvailableSeat);
            valueUpdate.Add("FlightTime",FlightTime);
            conn.Update("FlightInformation", valueUpdate, "FlightNo='" + ID + "'");
        }
    }
}
